home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / LabelPanel.java < prev    next >
Text File  |  1998-06-30  |  9KB  |  299 lines

  1. /*
  2.  * @(#)LabelPanel.java    1.6 98/01/31
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. import com.sun.java.swing.*;
  22. import com.sun.java.swing.text.*;
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.util.*;
  27.  
  28.  
  29. /**
  30.  * Labels!
  31.  *
  32.  * @version 1.1 11/14/97
  33.  * @author Jeff Dinkins
  34.  */
  35. public class LabelPanel extends JPanel 
  36. {
  37.     SwingSet swing;
  38.  
  39.     public LabelPanel(SwingSet swing) {
  40.     this.swing = swing;
  41.  
  42.     setBorder(swing.emptyBorder5);
  43.     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  44.     setOpaque(false);
  45.  
  46.     JLabel label;
  47.  
  48.     JPanel labelsPanel = SwingSet.createVerticalPanel(true);
  49.     labelsPanel.setOpaque(false);
  50.  
  51.     JPanel row1 = SwingSet.createHorizontalPanel(false);
  52.     JPanel row2 = SwingSet.createHorizontalPanel(false);
  53.     JPanel row3 = SwingSet.createHorizontalPanel(false);
  54.     JPanel row4 = SwingSet.createHorizontalPanel(false);
  55.     JPanel row5 = SwingSet.createHorizontalPanel(false);
  56.  
  57.         label = new BorderedSwingSetLabel("Label 1");
  58.     label.setToolTipText("Duke says \"Howdy!\"");
  59.         label.setHorizontalAlignment(label.CENTER);
  60.         label.setIcon(swing.dukeWave);
  61.         label.setVerticalTextPosition(label.BOTTOM);
  62.         label.setHorizontalTextPosition(label.CENTER);
  63.         row1.add(label);
  64.         swing.labels.addElement(label);
  65.  
  66.         label = new BorderedSwingSetLabel("Label2");
  67.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  68.         label.setHorizontalAlignment(label.CENTER);
  69.         label.setVerticalTextPosition(label.BOTTOM);
  70.         label.setHorizontalTextPosition(label.CENTER);
  71.         label.setFont(swing.boldFont);
  72.         label.setForeground(Color.red);
  73.         row1.add(label);
  74.         swing.labels.addElement(label);
  75.  
  76.         label = new BorderedSwingSetLabel("Label3");
  77.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  78.         label.setVerticalTextPosition(label.BOTTOM);
  79.         label.setHorizontalAlignment(label.CENTER);
  80.         label.setHorizontalTextPosition(label.CENTER);
  81.         row2.add(label);
  82.         label.setFont(swing.bigFont);
  83.         swing.labels.addElement(label);
  84.  
  85.  
  86.         label = new BorderedSwingSetLabel("Label4");
  87.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  88.         label.setVerticalTextPosition(label.BOTTOM);
  89.         label.setHorizontalAlignment(label.CENTER);
  90.         label.setHorizontalTextPosition(label.CENTER);
  91.         label.setIcon(swing.dukeMagnify);
  92.         row2.add(label);
  93.         label.setFont(swing.bigBoldFont);
  94.         swing.labels.addElement(label);
  95.  
  96.         label = new BorderedSwingSetLabel("Label 5");
  97.     label.setToolTipText("Shhhh.... Duke is taking a little nap.");
  98.         label.setVerticalTextPosition(label.BOTTOM);
  99.         label.setHorizontalAlignment(label.CENTER);
  100.         label.setHorizontalTextPosition(label.CENTER);
  101.         row3.add(label);
  102.         label.setForeground(Color.blue);
  103.         label.setIcon(swing.dukeSnooze);
  104.         label.setFont(swing.bigBoldFont);
  105.         swing.labels.addElement(label);
  106.  
  107.         label = new BorderedSwingSetLabel("Label 6");
  108.     label.setToolTipText("Yep! You can even have a ToolTip over a Label!.");
  109.         label.setVerticalTextPosition(label.BOTTOM);
  110.         label.setHorizontalAlignment(label.CENTER);
  111.         label.setHorizontalTextPosition(label.CENTER);
  112.         row3.add(label);
  113.         label.setFont(swing.reallyBigBoldFont);
  114.         label.setForeground(Color.green);
  115.         swing.labels.addElement(label);
  116.  
  117.         label = new JLabel("Type Here: ");
  118.         label.setHorizontalTextPosition(label.RIGHT);
  119.     label.setDisplayedMnemonic('T');
  120.     label.setToolTipText("The labelFor and displayedMnemonic properties work!");
  121.     JTextField tf = new JTextField("");
  122.     label.setLabelFor(tf);
  123.     tf.getAccessibleContext().setAccessibleName(label.getText());
  124.     row4.setBorder(swing.emptyBorder15);
  125.         row4.add(label);
  126.         row4.add(tf);
  127.         swing.labels.addElement(label);
  128.  
  129.         label = new JLabel("And Here: ");
  130.         label.setHorizontalTextPosition(label.RIGHT);
  131.     label.setDisplayedMnemonic('r');
  132.     label.setToolTipText("The labelFor and displayedMnemonic properties work!");
  133.     tf = new JTextField("");
  134.     label.setLabelFor(tf);
  135.     tf.getAccessibleContext().setAccessibleName(label.getText());
  136.     row5.setBorder(swing.emptyBorder15);
  137.         row5.add(label);
  138.         row5.add(tf);
  139.         swing.labels.addElement(label);
  140.  
  141.     // Add label panels to labelPanel
  142.     JPanel labelPanel = swing.createVerticalPanel(true);
  143.     labelPanel.setAlignmentX(LEFT_ALIGNMENT);
  144.     labelPanel.setAlignmentY(TOP_ALIGNMENT);
  145.  
  146.     labelPanel.add(row1);
  147.     labelPanel.add(row2);
  148.     labelPanel.add(row3);
  149.     labelPanel.add(row4);
  150.     labelPanel.add(row5);
  151.  
  152.     labelPanel.add(Box.createGlue());
  153.  
  154.  
  155.     // *************** Create the button controls ****************
  156.     JPanel controls = new JPanel() {
  157.         public Dimension getMaximumSize() {
  158.         return new Dimension(300, super.getMaximumSize().height);
  159.         }
  160.     };
  161.     controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
  162.     controls.setAlignmentY(TOP_ALIGNMENT);
  163.     controls.setAlignmentX(LEFT_ALIGNMENT);
  164.  
  165.     JPanel buttonControls = swing.createHorizontalPanel(true);
  166.     buttonControls.setAlignmentY(TOP_ALIGNMENT);
  167.     buttonControls.setAlignmentX(LEFT_ALIGNMENT);
  168.  
  169.     JPanel leftColumn = swing.createVerticalPanel(false);
  170.     leftColumn.setAlignmentX(LEFT_ALIGNMENT);
  171.     leftColumn.setAlignmentY(TOP_ALIGNMENT);
  172.  
  173.     JPanel rightColumn = swing.createVerticalPanel(false);
  174.     rightColumn.setAlignmentX(LEFT_ALIGNMENT);
  175.     rightColumn.setAlignmentY(TOP_ALIGNMENT);
  176.  
  177.     buttonControls.add(leftColumn);
  178.     buttonControls.add(Box.createRigidArea(swing.hpad20));
  179.     buttonControls.add(rightColumn);
  180.     buttonControls.add(Box.createRigidArea(swing.hpad20));
  181.  
  182.     controls.add(buttonControls);
  183.  
  184.     // Display Options
  185.     JLabel l = new JLabel("Display Options:");
  186.     leftColumn.add(l);
  187.     l.setFont(swing.boldFont);
  188.  
  189.     JCheckBox bordered = new JCheckBox("Paint Border");
  190.     bordered.setEnabled(false);
  191.         bordered.setMnemonic('b');
  192.      bordered.addItemListener(swing.buttonDisplayListener);
  193.      leftColumn.add(bordered);
  194.  
  195.      JCheckBox focused = new JCheckBox("Paint Focus");
  196.     focused.setEnabled(false);
  197.         focused.setMnemonic('f');
  198.      focused.addItemListener(swing.buttonDisplayListener);
  199.      leftColumn.add(focused);
  200.  
  201.     JCheckBox enabled = new JCheckBox("Enabled");
  202.     enabled.setToolTipText("Click here to enable or disable all labels.");
  203.     enabled.setSelected(true);
  204.         enabled.setMnemonic('e');
  205.     enabled.addItemListener(swing.buttonDisplayListener);
  206.     leftColumn.add(enabled);
  207.  
  208.  
  209.     leftColumn.add(Box.createRigidArea(swing.vpad20));
  210.  
  211.     l = new JLabel("Pad Amount:");
  212.     l.setEnabled(false);
  213.     leftColumn.add(l);
  214.     l.setFont(swing.boldFont);
  215.     
  216.     ButtonGroup group = new ButtonGroup();
  217.     JRadioButton defaultPad = new JRadioButton("Default");
  218.     defaultPad.setEnabled(false);
  219.         defaultPad.setMnemonic('d');
  220.     group.add(defaultPad);
  221.     defaultPad.setSelected(true);
  222.      defaultPad.addItemListener(swing.buttonPadListener);
  223.     leftColumn.add(defaultPad);
  224.  
  225.     JRadioButton zeroPad = new JRadioButton("0");
  226.         zeroPad.setMnemonic('0');
  227.     zeroPad.setEnabled(false);
  228.     group.add(zeroPad);
  229.      zeroPad.addItemListener(swing.buttonPadListener);
  230.     leftColumn.add(zeroPad);
  231.  
  232.     JRadioButton tenPad = new JRadioButton("10");
  233.         tenPad.setMnemonic('1');
  234.     tenPad.setEnabled(false);
  235.     group.add(tenPad);
  236.      tenPad.addItemListener(swing.buttonPadListener);
  237.     leftColumn.add(tenPad);
  238.     
  239.     // *************** Create the layout controls ****************
  240.     // Create Text Position Layout control
  241.     JPanel textPosition = DirectionButton.createDirectionPanel(true, "S", swing.textPositionListener);
  242.     JPanel labelAlignment = DirectionButton.createDirectionPanel(true, "C", swing.labelAlignmentListener);
  243.  
  244.     l = new JLabel("Text Position:");
  245.     rightColumn.add(l);
  246.     l.setFont(swing.boldFont);
  247.      rightColumn.add(textPosition);
  248.  
  249.      rightColumn.add(Box.createRigidArea(swing.vpad20));
  250.  
  251.     l = new JLabel("Content Alignment:");
  252.     rightColumn.add(l);
  253.     l.setFont(swing.boldFont);
  254.      rightColumn.add(labelAlignment);
  255.  
  256.      rightColumn.add(Box.createGlue());
  257.  
  258.     add(labelPanel);
  259.     add(Box.createRigidArea(swing.hpad10));
  260.      add(controls);
  261.     }
  262.  
  263.  
  264.     class BorderedSwingSetLabel extends JLabel {
  265.  
  266.     BorderedSwingSetLabel(String text) {
  267.         super(text);
  268.     }
  269.  
  270.     public Insets getInsets() {
  271.         Insets insets = super.getInsets();
  272.         insets.left += 3;
  273.         insets.right += 3;
  274.         insets.top += 3;
  275.         insets.bottom += 3;
  276.         return insets;
  277.     }
  278.  
  279.     public float getAlignX() {
  280.         return LEFT_ALIGNMENT;
  281.     }
  282.  
  283.     public Dimension getPreferredSize() {
  284.         return new Dimension(145, 90);
  285.     }
  286.  
  287.     public Dimension getMaximumSize() {
  288.         return new Dimension(250, 160);
  289.     }
  290.  
  291.     public void paint(Graphics g) {
  292.         super.paint(g);
  293.         g.setColor(Color.black);
  294.         g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
  295.     }
  296.     }
  297.     
  298. }
  299.